{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "dc6e3890",
   "metadata": {},
   "source": [
    "# OCR（光学字符识别）\n",
    "## 学生实践，并观察res结果\n",
    "> 1. 通用的场景文字识别\n",
    ">> 1. 通用文字识别\n",
    ">> 2. 网络图片文字识别 \n",
    ">> 3. 手写文字识别\n",
    ">> 4. 二维码识别（二维码转文本）\n",
    "\n",
    "> 2. 卡片文本识别\n",
    ">> 1. 身份证\n",
    ">> 2. 银行卡\n",
    "\n",
    "> 3. 财务票据文字识别\n",
    ">> 1. 增值税发票\n",
    ">> 2. 火车票\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b8613b2a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 获取access token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "50930d94",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.a14d05a846fd1b927b19df3782fca7eb.315360000.1965197477.282335-25949044', 'expires_in': 2592000, 'session_key': '9mzdAqAdQlSqGuMm6zTJg3GK/wSxcfoidJh958GpWFQ2/MZccEg0PfYVH0hCUWUzkA0jmER+aCBt4sd7Y8fyu7zxm9yrYA==', 'access_token': '24.9b792311228f6497246116e8d772ec0b.2592000.1652429477.282335-25949044', 'scope': 'public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_qrcode brain_ocr_handwriting brain_form brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate vis-ocr_台湾通行证 vis-ocr_港澳通行证 vis-ocr_机动车购车发票识别 vis-ocr_机动车检验合格证识别 vis-ocr_车辆vin码识别 vis-ocr_定额发票识别 vis-ocr_保单识别 vis-ocr_机打发票识别 vis-ocr_行程单识别 brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_invoice brain_ocr_vehicle_certificate brain_ocr_air_ticket brain_ocr_invoice brain_ocr_insurance_doc brain_formula brain_seal brain_ocr_facade brain_ocr_meter brain_doc_analysis brain_ocr_webimage_loc brain_ocr_medical_paper brain_ocr_doc_analysis_office brain_vat_invoice_verification brain_ocr_medical_detail brain_vehicle_registration_certificate brain_ocr_online_taxi_itinerary brain_ocr_multi_idcard brain_ocr_mixed_multi_vehicle brain_ocr_weigth_note brain_ocr_ multiple_invoice wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online', 'session_secret': '3d904802bef7aad59710bf2e0ed17454'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'hRbWD3X4nVY5h3riy6LAULs2',\n",
    "    'client_secret':'EGmFGBdGCnjWOAPfMY9FqO4kQXlSBkOe'\n",
    "}\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f2ce5c83",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'refresh_token': '25.a14d05a846fd1b927b19df3782fca7eb.315360000.1965197477.282335-25949044',\n",
       " 'expires_in': 2592000,\n",
       " 'session_key': '9mzdAqAdQlSqGuMm6zTJg3GK/wSxcfoidJh958GpWFQ2/MZccEg0PfYVH0hCUWUzkA0jmER+aCBt4sd7Y8fyu7zxm9yrYA==',\n",
       " 'access_token': '24.9b792311228f6497246116e8d772ec0b.2592000.1652429477.282335-25949044',\n",
       " 'scope': 'public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_qrcode brain_ocr_handwriting brain_form brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate vis-ocr_台湾通行证 vis-ocr_港澳通行证 vis-ocr_机动车购车发票识别 vis-ocr_机动车检验合格证识别 vis-ocr_车辆vin码识别 vis-ocr_定额发票识别 vis-ocr_保单识别 vis-ocr_机打发票识别 vis-ocr_行程单识别 brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_invoice brain_ocr_vehicle_certificate brain_ocr_air_ticket brain_ocr_invoice brain_ocr_insurance_doc brain_formula brain_seal brain_ocr_facade brain_ocr_meter brain_doc_analysis brain_ocr_webimage_loc brain_ocr_medical_paper brain_ocr_doc_analysis_office brain_vat_invoice_verification brain_ocr_medical_detail brain_vehicle_registration_certificate brain_ocr_online_taxi_itinerary brain_ocr_multi_idcard brain_ocr_mixed_multi_vehicle brain_ocr_weigth_note brain_ocr_ multiple_invoice wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online',\n",
       " 'session_secret': '3d904802bef7aad59710bf2e0ed17454'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7742280c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.9b792311228f6497246116e8d772ec0b.2592000.1652429477.282335-25949044'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "coco_AT = response.json()['access_token']\n",
    "coco_AT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a07266cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 通用的场景文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3c8b6e08",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [{'words': 'Study'}, {'words': '别看手机'}, {'words': '滚去学习'}, {'words': 'Study'}], 'words_result_num': 4, 'log_id': 1514154256957369548}\n"
     ]
    }
   ],
   "source": [
    "# 通用文字识别\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用文字识别（高精度版）\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('word1.jpeg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = coco_AT\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'access_token':coco_AT,\n",
    "    'image':img,\n",
    "    'language_type':\"auto_detect\"\n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "cca70051",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'language': -1, 'words_result': [{'words': '不是必需品不买'}, {'words': '攒钱!'}, {'words': '存下的才是赚到的'}, {'words': '攒钱'}, {'words': '别乱花余额不足'}, {'words': '不买立省100%'}, {'words': '攒钱'}, {'words': '用不买买来也没用'}], 'words_result_num': 8, 'log_id': 1514154259807695960}\n"
     ]
    }
   ],
   "source": [
    "# 网络图片文字识别\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "网络图片文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/webimage\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('word2.jpeg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = coco_AT\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'access_token':coco_AT,\n",
    "    'image':img,\n",
    "    'detect_language':'true'\n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7fae82b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': [], 'words_result_num': 0, 'log_id': 1514154264375177603}\n"
     ]
    }
   ],
   "source": [
    "# 手写文字识别\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "手写文字识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('word3.jpeg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = coco_AT\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'access_token':coco_AT,\n",
    "    'image':img\n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19f7c4fd",
   "metadata": {},
   "source": [
    "[二维码生成器](https://cli.im/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c9e904b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'codes_result': [{'text': ['���'], 'type': 'QR_CODE'}], 'codes_result_num': 1, 'log_id': 1514154280421519937}\n"
     ]
    }
   ],
   "source": [
    "# 二维码识别（二维码转文本）\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "二维码识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/qrcode\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('word_code.png', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = coco_AT\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'access_token':coco_AT,\n",
    "    'image':img\n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8252d0ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 卡片文本识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "6c952b63",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': {'姓名': {'location': {'top': 94, 'left': 130, 'width': 61, 'height': 24}, 'words': '奥巴马'}, '民族': {'location': {'top': 128, 'left': 231, 'width': 60, 'height': 23}, 'words': '哈尼'}, '住址': {'location': {'top': 202, 'left': 132, 'width': 191, 'height': 62}, 'words': '盛顿特区宾夕法尼亚大道1600号白宫'}, '公民身份号码': {'location': {'top': 0, 'left': 0, 'width': 0, 'height': 0}, 'words': ''}, '出生': {'location': {'top': 164, 'left': 127, 'width': 153, 'height': 21}, 'words': '19610804'}, '性别': {'location': {'top': 128, 'left': 127, 'width': 20, 'height': 23}, 'words': '男'}}, 'words_result_num': 6, 'idcard_number_type': 0, 'image_status': 'unknown', 'log_id': 1514156613868853615}\n"
     ]
    }
   ],
   "source": [
    "# 身份证识别\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "身份证识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/idcard\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('IDcard.jpeg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = coco_AT\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'access_token':coco_AT,\n",
    "    'image':img,\n",
    "    'id_card_side':'front'\n",
    "   \n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "6e5bda46",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result': {'失效日期': {'location': {'top': 256, 'left': 287, 'width': 54, 'height': 15}, 'words': '20150829'}, '签发机关': {'location': {'top': 233, 'left': 219, 'width': 72, 'height': 15}, 'words': '桐乡市公安局'}, '签发日期': {'location': {'top': 257, 'left': 222, 'width': 56, 'height': 14}, 'words': '20050829'}}, 'words_result_num': 3, 'image_status': 'normal', 'log_id': 1514157279501488024}\n"
     ]
    }
   ],
   "source": [
    "# 身份证识别\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "身份证识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/idcard\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('idcard2.jpeg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = coco_AT\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'access_token':coco_AT,\n",
    "    'image':img,\n",
    "    'id_card_side':'back'\n",
    "   \n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "8f7935cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': {'valid_date': '07/26', 'bank_card_number': '621226 1612004897709', 'bank_name': '工商银行', 'bank_card_type': 1, 'holder_name': ''}, 'direction': 0, 'log_id': 1514158366198139492}\n"
     ]
    }
   ],
   "source": [
    "# 银行卡识别\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "银行卡识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/bankcard\"\n",
    "\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('bankcard.jpeg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = coco_AT\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'access_token':coco_AT,\n",
    "    'image':img\n",
    "   \n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "b21e69de",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 财务票据文字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "40af63cb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result_num': 43, 'words_result': {'AmountInWords': '伍圆整', 'InvoiceNumConfirm': '', 'CommodityEndDate': [], 'CommodityVehicleType': [], 'CommodityStartDate': [], 'CommodityPrice': [{'row': '1', 'word': '4.72'}], 'NoteDrawer': 'GZ3133', 'SellerAddress': '广东省广州市环市东路369号友谊商业大厦13楼020-83588288', 'CommodityNum': [{'row': '1', 'word': '1'}], 'SellerRegisterNum': '9144000061740323XQ', 'MachineCode': '661616233898', 'Remarks': '', 'SellerBank': '中国建设银行股份有限公司广州帝景大厦支行44001491204050486509', 'CommodityTaxRate': [{'row': '1', 'word': '6%'}], 'TotalTax': '0.28', 'InvoiceCodeConfirm': '044002000511', 'CheckCode': '60692341610291115071', 'InvoiceCode': '044002000511', 'InvoiceDate': '2022年03月21日', 'PurchaserRegisterNum': '', 'InvoiceTypeOrg': '广东增值税电子普通发票', 'OnlinePay': '', 'Password': '5+27893>*-2>*+52>*8/00>99103829485308*93566>-5+*9>172-50-27<90<>/>3822*+148*9/*0/99/+2+/9485308*93566>-5*5*-', 'Agent': '否', 'AmountInFiguers': '5.00', 'PurchaserBank': '', 'Checker': '殷瑛', 'City': '', 'TotalAmount': '4.72', 'CommodityAmount': [{'row': '1', 'word': '4.72'}], 'PurchaserName': '黄可', 'CommodityType': [], 'Province': '广东', 'InvoiceType': '电子普通发票', 'SheetNum': '', 'PurchaserAddress': '', 'CommodityTax': [{'row': '1', 'word': '0.28'}], 'CommodityPlateNum': [], 'CommodityUnit': [], 'Payee': '张智雯', 'CommodityName': [{'row': '1', 'word': '*餐饮服务*餐饮服务'}], 'SellerName': '百胜餐饮(广东)有限公司', 'InvoiceNum': '68346601'}, 'log_id': 1514160054994315169}\n"
     ]
    }
   ],
   "source": [
    "# 增值税发票识别\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "增值税发票识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/vat_invoice\"\n",
    "\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('invoice.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = coco_AT\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'access_token':coco_AT,\n",
    "    'image':img\n",
    "   \n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "849f8d2c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'words_result_num': 13, 'words_result': {'id_num': '1301231997****0015', 'serial_number': '65870300070929X006460', 'ticket_num': 'X006460', 'seat_num': '01车047号', 'time': '13:50', 'date': '2019年09月29日', 'name': '高海港', 'destination_station': '石家庄站', 'seat_category': '新空调硬座', 'starting_station': '广州站', 'sales_station': '汕尾', 'train_num': 'Z90', 'ticket_rates': '￥224.0元'}, 'direction': 0, 'log_id': 1514161902038376483}\n"
     ]
    }
   ],
   "source": [
    "# 火车票识别\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "火车票识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/ocr/v1/train_ticket\"\n",
    "\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('ticket.jpeg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = coco_AT\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'access_token':coco_AT,\n",
    "    'image':img\n",
    "   \n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebd7b236",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
